package com.ivc.trade.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cs.framework.common.util.CommonUtils;
import com.ivc.info.dto.StockInOutDto;
import com.ivc.info.service.StockManageService;
import com.ivc.rule.util.RuleConstant;
import com.ivc.trade.dto.TradeEnterDto;
import com.ivc.trade.dto.base.BuyInfoDto;
import com.ivc.trade.dto.base.SellInfoDto;
import com.ivc.trade.service.BuyInfoService;
import com.ivc.trade.service.SellInfoService;
import com.ivc.trade.service.TradeEnterService;

/**
 * 交易管理
 * 
 * @author hedongzhou
 * 
 */
@Service("tradeEnterService")
public class TradeEnterServiceImpl implements TradeEnterService {

	@Resource
	private BuyInfoService buyInfoService;

	@Resource
	private SellInfoService sellInfoService;

	@Resource
	private StockManageService stockManageService;

	/**
	 * 新增进货信息
	 * 
	 * @param traEntergeDto
	 */
	public void saveBuyInfo(TradeEnterDto tradeEnterDto) {
		List<BuyInfoDto> buyInfo = tradeEnterDto.getBuyInfo();

		if (CommonUtils.isEmpty(buyInfo)) {
			CommonUtils.throwRun("进货信息不能为空");
		}

		for (BuyInfoDto entity : buyInfo) {
			entity = buyInfoService.save(entity);

			StockInOutDto stockInOutDto = new StockInOutDto();
			CommonUtils.copyBoValue(stockInOutDto, entity);

			stockInOutDto.setBusinessNo(entity.getBuyInfoNo());
			stockInOutDto.setBusinessType(RuleConstant.BusinessType.buy);

			stockManageService.inOrOut(stockInOutDto);
		}
	}

	/**
	 * 新增销售信息
	 * 
	 * @param tradeEnterDto
	 */
	public void saveSellInfo(TradeEnterDto tradeEnterDto) {
		List<SellInfoDto> sellInfo = tradeEnterDto.getSellInfo();

		if (CommonUtils.isEmpty(sellInfo)) {
			CommonUtils.throwRun("销售信息不能为空");
		}

		for (SellInfoDto entity : sellInfo) {
			entity = sellInfoService.save(entity);

			StockInOutDto stockInOutDto = new StockInOutDto();
			CommonUtils.copyBoValue(stockInOutDto, entity);

			stockInOutDto.setBusinessNo(entity.getSellInfoNo());
			stockInOutDto.setBusinessType(RuleConstant.BusinessType.sell);
			stockInOutDto.setQuantity(-stockInOutDto.getQuantity());

			stockManageService.inOrOut(stockInOutDto);
		}
	}

}
